package com.caiyouxi.finance.service;

import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.cache.RedisService;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.common.util.Log;
import com.caiyouxi.finance.mapper.FinanceCurrencyMapper;
import com.caiyouxi.finance.model.FinanceCurrency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 货币类型
 * chensong
 */
@Service
public class FinanceCurrencyService extends BaseService {
    
    @Autowired
    private FinanceCurrencyMapper financeCurrencyMapper;
    @Autowired
    private RedisService redisService;


    public FinanceCurrency get_finance_currency_by_code(String code)
    {
        String redis_key = "FINANCE_CURRENCY_"+"_"+code;
        Log.d("search redis,key:"+redis_key);
        FinanceCurrency bean = null;
        if(null==bean)
        {
            Log.d("search db...");
            bean = financeCurrencyMapper.select_finance_currency(code);
            redisService.set(redis_key,bean);
        }
        return bean;
    }

    public FinanceCurrency get_fcurrency_by_buying_type(String buying_code)
    {
        String redis_key = "FINANCE_CURRENCY_BUY_"+"_"+buying_code;
        Log.d("search redis,key:"+redis_key);
        FinanceCurrency bean = null;
        if(null==bean)
        {
            Log.d("search db...");
            bean = financeCurrencyMapper.get_fcurrency_by_buying_type(buying_code);
            redisService.set(redis_key,bean);
        }
        return bean;
    }


    public ResData add_finance_currency(FinanceCurrency financeCurrency){
        return success(financeCurrencyMapper.add_finance_currency(financeCurrency));
    }

    public void update_finance_currency(FinanceCurrency financeCurrency){
        financeCurrencyMapper.update_finance_currency(financeCurrency);
    }

    public void delete_finance_currency(FinanceCurrency financeCurrency){
        financeCurrencyMapper.delete_finance_currency(financeCurrency);
    }

    /**
     * 分页查询
     * */
    public ResData finance_currency_list(FinanceCurrency bean){
        QueryParams pageList = new QueryParams();
        if(bean.getPage_size() == 0){
            bean.setPage_size(Integer.parseInt(redisService.get("sys.pagesize").toString()));
        }
        pageList.setPage_size(bean.getPage_size());
        pageList.setTotal(bean.getTotal());
        pageList.setList(financeCurrencyMapper.finance_currency_list(bean));
        pageList.setTotal(financeCurrencyMapper.finance_currency_counts(bean));
        return success(pageList);
    }


}
